package cn.ffcs.uec.outStaffAdjustApply.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.common.vo.UecBdPsncl;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uec.outStaffAdjustApply.component.bean.OutStaffAdjustApplyListboxExtBean;
import cn.ffcs.uec.post.dao.UecPostDao;
import cn.ffcs.uec.post.manager.UecPostManager;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uec.post.vo.BasePostVo;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.manager.UecPsnjobManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecPsnjob;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * 外包人员调配管理
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年8月14日
 */
@Controller
@Scope("prototype")
public class OutStaffAdjustApplyListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5719638301202841724L;

	private static final String OUT_STAFF_ADJUST_APPLY_LISTBOX_EXT_ZUL = "/pages_nmg/outStaffAdjustApply/comp/outStaffAdjustApply_listbox_ext.zul";
	// 部门第一负责人
	public static final String HEAD_OF_DEPARTMENT = "1001AA100000001ZIHCY";
	// 部门其他负责人
	public static final String OTHER_OF_DEPARTMENT = "1001AA100000001ZIHCZ";
	// 专业序列
	public static final String PROFESSIONAL_SEQ = "1001A1100000000006R3";
	// 管理序列
	public static final String MANAGERMENT_SEQ = "1001A1100000000006R2";

	/**
	 * 页面对应的bean
	 */
	@Getter
	@Setter
	private OutStaffAdjustApplyListboxExtBean bean = new OutStaffAdjustApplyListboxExtBean();

	private UecPostManager uecPostManager = (UecPostManager) ApplicationContextUtil.getBean("uecPostManager");

	/**
	 * 外包人员基本信息对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	private UecOutStaffInfo qryUecOutStaffInfo;
	/**
	 * 外包人员虚拟对象
	 */
	private UecOutStaffInfoVo uecOutStaffInfoVo;

	/**
	 * 任职信息
	 */
	private UecPsnjob uecPsnjob;
	private UecPsnjob qryUecPsnjob;
	/**
	 * 人员类别
	 */
	private UecBdPsncl uecBdPsncl;
	/**
	 * 人员类别树
	 */
	private UecBdPsncl uecBdPsnclTree;

	/**
	 * 岗位
	 */
	private UecPost uecPost;
	/**
	 * 组织
	 */
	private UecOrgDept findUecOrgDept;

	/**
	 * 选择的组织
	 */
	private OrganizationRelationVo organization;
	/**
	 * 选择的基准岗位 三级
	 */
	private BasePostVo basePostVo;
	/**
	 * 二级基准岗
	 */
	private BasePostVo basePostVoB;
	/**
	 * 一级基准岗
	 */
	private BasePostVo basePostVoA;
	/**
	 * 外包人员基本信息更新标志
	 */
	private boolean outStaffInfoUpdateFlag = false;
	
	private String postSequence = null;
	
	private String basePostCodeA = null;
	
	private String basePostCodeB = null;
	
	private String basePostCodeC = null;
	//基准岗位主键 == 三级基准岗位主键
	private String pkBasePostC = null;
	//二级基准岗位主键
	private String pkBasePostB = null;
	//一级基准岗位主键
	private String pkBasePostA = null;

	private UecPostDao uecPostDao = (UecPostDao) ApplicationContextUtil.getBean("uecPostDao");

	private UecPsnjobManager uecPsnjobManager = (UecPsnjobManager) ApplicationContextUtil.getBean("uecPsnjobManager");

	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil.getBean("uecTrialManager");

	private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
			.getBean("customFilesManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public OutStaffAdjustApplyListboxExt() {
		Executions.createComponents(OUT_STAFF_ADJUST_APPLY_LISTBOX_EXT_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.bean.getUecPostBandboxExt().addForward("onUecPostEditSelected", this, "onUecPostEditSelectedResponse");
		this.bean.getOutStaffPkDept().addForward("onGetOrganization", this, "onGetOrganizationResponse");
//		this.bean.getOutStaffBasePost().addForward("onGetBasePost", this, "onGetBasePostResponse");
		this.bean.getOutPsncl().addForward("onGetBdPsncl", this, "onGetBdPsnclResponse");
	}

	/**
	 * 界面初始化
	 */
	public void onCreate() {
		bandListbox();
		bean.getUecPostBandboxExt().setDisabled(true);
	}

	/**
	 * 监听人员类别组织树
	 */
	public void onGetBdPsnclResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			uecBdPsnclTree = (UecBdPsncl) event.getOrigin().getData();
		}
	}

	/**
	 * 监听--领导职务
	 */
	public void onSelect$jobglbdef25() {
		String pkPsdoc = (String) bean.getJobglbdef25().getSelectedItem().getValue();
		/**
		 * 如果领导职务为：部门第一负责人或者部门其他负责人，负责部门取部门
		 */
		if (HEAD_OF_DEPARTMENT.equals(pkPsdoc) || OTHER_OF_DEPARTMENT.equals(pkPsdoc)) {
			if (null != findUecOrgDept) {
				bean.getJobglbdef26().setValue(findUecOrgDept.getName());
			}
		} else {
			bean.getJobglbdef26().setValue(null);
		}
	}

	/**
	 * 监听组织树插件抛出的事件
	 * 
	 * @param event
	 */
	public void onGetOrganizationResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			organization = (OrganizationRelationVo) event.getOrigin().getData();
			if ("ORG".equals(organization.getOrgType())) {
				ZkUtil.showError("调配后部门选项请选择到部门!", "错误提示");
				bean.getOutStaffPkDept().setValue(null);
				bean.getHrorg().setValue(null);
				bean.getUecPostBandboxExt().setValue(null);
				bean.getPkPostseries().setValue(null);
				bean.getJobglbdef22().setValue(null);
				bean.getJobglbdef23().setValue(null);
				bean.getJobglbdef24Name().setValue(null);
				bean.getUecPostBandboxExt().setDisabled(true);
				return;
			} else {
				// 根据部门主键：pkorg，找到该部门所在的公司
				findUecOrgDept = new UecOrgDept();
				findUecOrgDept.setPkDept(organization.getOrg());
				findUecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrgDept(findUecOrgDept);
				String orgNametemp="";
				if (null != findUecOrgDept) {
					String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
					String uecOrgOrgsNameByPkOrg = uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(findUecOrgDept.getPkOrg());
					orgNametemp = PermissionUtil.getManagerOrg();
					String[] orgnamelist= orgNametemp.split(",");
					boolean   temp=false;
					if(code.startsWith("W2019")) {
						      for(int i=0;i<orgnamelist.length;i++) {
								if( !uecOrgOrgsNameByPkOrg.contains(orgnamelist[i])) {
								  temp=true;
								  break;
								}
						}
					}
					if(temp) {
						return;
					}	  
					bean.getUecPostBandboxExt().setDisabled(false);
					bean.getHrorg().setValue(uecOrgOrgsNameByPkOrg);
					bean.getUecPostBandboxExt().setValue(null);
					Events.postEvent("onUecDeptSelect",
							this.bean.getUecPostBandboxExt().getBean().getUecPostListboxExt(), findUecOrgDept);
				
			}
		}
	}
	}
	/**
	 * 监听UecOrgPostBandboxExt传出的事件
	 * 
	 * @param event
	 */
	public void onUecPostEditSelectedResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			uecPost = (UecPost) event.getOrigin().getData();
			if (null != uecPost) {
				//岗位--页面岗位赋值
				bean.getUecPostBandboxExt().setValue(uecPost.getPostname());
				// 岗位序列 --页面赋值
				postSequence = uecPost.getPkPostseries();
				if ("1001A1100000000006R2".equals(postSequence)) {
					bean.getPkPostseries().setValue("管理序列");
				} else if ("1001A1100000000006R3".equals(postSequence)) {
					bean.getPkPostseries().setValue("专业序列");
				}
				// 判断该岗位的基准岗位主键是否为空
				if (!StrUtil.isEmpty(uecPost.getPkPoststd())) {
					//基准岗位主键 == 三级基准岗位主键
					pkBasePostC = uecPost.getPkPoststd();
					// 根据基准岗位主键查出基准岗位编码
					UecPost uecBasePostC = UecPost.newInstance();
					uecBasePostC.setPkPoststd(pkBasePostC);
					uecBasePostC = uecPostDao.queryBasePostByUecPost(uecBasePostC);
					if(uecBasePostC != null) {
						//三级基准岗编码
						basePostCodeC = uecBasePostC.getPostcode();
						//三级基准岗名称
						bean.getJobglbdef24Name().setValue(uecBasePostC.getPostname());
						//获取二级基准岗位编码
						basePostCodeB = uecPostDao.getPreviousCodeByCode(basePostCodeC);
						if(!StrUtil.isNullOrEmpty(basePostCodeB)) {
							UecPost uecBasePostB = UecPost.newInstance();
							uecBasePostB = uecPostDao.getPkPostByCode(basePostCodeB);
							if(uecBasePostB != null) {
								//二级基准岗位编码
								basePostCodeB = uecBasePostB.getPostcode();
								//二级基准岗位主键
								pkBasePostB = uecBasePostB.getPkPost();
								//二级基准岗名称
								bean.getJobglbdef23().setValue(uecBasePostB.getPostname());
								//获取一级基准岗位编码
								basePostCodeA = uecPostDao.getPreviousCodeByCode(basePostCodeB);
								if(!StrUtil.isNullOrEmpty(basePostCodeA)) {
									UecPost uecBasePostA = UecPost.newInstance();
									uecBasePostA = uecPostDao.getPkPostByCode(basePostCodeA);
									if(uecBasePostA != null) {
										//二级基准岗位编码
										basePostCodeA = uecBasePostA.getPostcode();
										//二级基准岗位主键
										pkBasePostA = uecBasePostA.getPkPost();
										//二级基准岗名称
										bean.getJobglbdef22().setValue(uecBasePostA.getPostname());
									}
								}
							}
						}			
					}
				}
			}
		}
	}

	/**
	 * 监听基准岗位参考树
	 * 
	 * @param event
	 */
	public void onGetBasePostResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			basePostVo = (BasePostVo) event.getOrigin().getData();
			if (!"3".equals(basePostVo.getLv())) {
				ZkUtil.showError("基准岗位请选择到最后一级!", "错误提示");
				bean.getOutStaffBasePost().setValue(null);
				bean.getJobglbdef22().setValue(null);
				bean.getJobglbdef23().setValue(null);
				return;
			}
			// 根据所选择的的第三级基准岗位，查出第二级、第一级基准岗位，并赋值
			// 获取第三级基准岗位编码
			// String codeC = basePostVo.getCode();
			// 获取第三级基准岗位的上级编码
			String preCodeC = basePostVo.getPcode();
			// 根据三级基准岗位获取二级基准岗位
			basePostVoB = uecPostDao.getPreviousBasePostByCode(preCodeC);
			if (basePostVoB != null) {
				bean.getJobglbdef23().setValue(basePostVoB.getPostname());
				// 根据二级基准岗位获取一级基准岗位
				basePostVoA = uecPostDao.getPreviousBasePostByCode(basePostVoB.getPcode());
				if (basePostVoA != null) {
					bean.getJobglbdef22().setValue(basePostVoA.getPostname());
				}
			}
		}
	}

	/**
	 * 查询按钮 根据条件查询
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void onQuery() throws SuspendNotAllowedException, Exception {
		clearVariable();
		uecOutStaffInfoVo = new UecOutStaffInfoVo();
		/**
		 * 组织树Ext UecOrganizationTreeBandboxExt 组织树上有组织和部门 通过组织树获取组织或部门
		 */
		if (!StrUtil.isNullOrEmpty(bean.getOutStaffPkHrorg().getValue())) {
			// 组织类型：公司 or 部门
			String orgType = (String) bean.getOutStaffPkHrorg().getOrganization().getOrgType();
			// 组织主键
			String pkorg = (String) bean.getOutStaffPkHrorg().getOrganization().getOrg();
			if ("ORG".equals(orgType)) {
				String orgName = (String) bean.getOutStaffPkHrorg().getOrganization().getOrgName();
				// 公司
				//uecOutStaffInfoVo.setPkOrg(pkorg);
				 if("中国电信股份有限公司内蒙古分公司".equals(orgName)) {
			       		uecOutStaffInfoVo
			       		.setOrgName(null);
			       	}else if(orgName.endsWith("*")){
			       		uecOutStaffInfoVo
			       		.setOrgName(orgName);
			       	}else {
			       		uecOutStaffInfoVo.setPkOrg(pkorg);
			       	}
			} else {
				// 部门
				UecOrgDept uecOrgDept = new UecOrgDept();
				UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
				uecOrgDept.setPkDept(pkorg);
				uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrgDept(uecOrgDept);
				// 部门所属公司
				if(uecOrgDept !=null) {
					uecOrgOrgs= new UecOrgOrgs();
					uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
					uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
					String orgName=uecOrgOrgs.getName();
					uecOutStaffInfoVo.setPkDept(uecOrgDept.getPkDept());
					uecOutStaffInfoVo.setPkOrg(uecOrgDept.getPkOrg());
					uecOutStaffInfoVo.setOrgName(orgName);

				//uecOutStaffInfoVo.setPkDept(pkorg);
			}
			}
		}
		uecOutStaffInfoVo.setStaffCode(bean.getOutStaffCode().getValue());
		uecOutStaffInfoVo.setStaffName(bean.getOutStaffName().getValue());
		uecOutStaffInfoVo.setIdNum(bean.getOutStaffIdNum().getValue());
		// 查询员工
		outStaffList();
	}

	/**
	 * 员工列表
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */

	public void outStaffList() throws SuspendNotAllowedException, Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", "outStaffList");
		map.put("uecOutStaffInfoVo", uecOutStaffInfoVo);
		Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_LIST_ZUL, this, map);
		window.doModal();
		window.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getData() != null) {
					qryUecOutStaffInfo = (UecOutStaffInfo) event.getData();

					UecTrial qryUecTrial = UecTrial.newInstance();
					qryUecTrial.setStaffId(qryUecOutStaffInfo.getOutStaffId());
					qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
					qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
					if (null == qryUecTrial) {
						ZkUtil.showError("此人未入职,请先执行入职操作", "温馨提示");
						return;
					}
					bean.getPsnName().setValue(qryUecOutStaffInfo.getPsnName());
					bean.getPsnCode().setValue(qryUecOutStaffInfo.getPsnCode());
					// 任职信息中有岗位
					qryUecPsnjob = UecPsnjob.newInstance();
					qryUecPsnjob.setStaffId(qryUecOutStaffInfo.getOutStaffId());
					qryUecPsnjob.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
					qryUecPsnjob = uecPsnjobManager.findUecPsnjob(qryUecPsnjob);
					if (null == qryUecPsnjob) {
						ZkUtil.showError("任职信息为空!", "错误提示");
						return;
					} else {
						if (!StrUtil.isNullOrEmpty(qryUecPsnjob.getPkOrg())) {
							// 调配前信息
							// 组织
							bean.getOrgName()
									.setValue(uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(qryUecPsnjob.getPkOrg()));
							// 部门
							bean.getDeptName()
									.setValue(uecOrgDeptManager.getUecOrgDeptNameByPkDept(qryUecPsnjob.getPkDept()));
							// 岗位
							bean.getPostName()
									.setValue(uecPostManager.getUecPostNameByPkPost(qryUecPsnjob.getPkPost()));
							// 人员类别
							uecBdPsncl = new UecBdPsncl();
							uecBdPsncl.setPkPsncl(qryUecPsnjob.getPkPsncl());
							uecBdPsncl = customFilesManager.findUecUecBdPsnclByUecBdPsncl(uecBdPsncl);
							if (!StrUtil.isNullOrEmpty(uecBdPsncl)) {
								bean.getPersonnelCategory().setValue(uecBdPsncl.getName());
							}
							// 岗位序列
							if (PROFESSIONAL_SEQ.equals(qryUecPsnjob.getPkPostseries())) {
								bean.getPostSequence().setValue("专业序列");
							} else if (MANAGERMENT_SEQ.equals(qryUecPsnjob.getPkPostseries())) {
								bean.getPostSequence().setValue("管理序列");
							} else {
								bean.getPostSequence().setValue(null);
							}
							// 岗位层级
							//bean.getPostLevel().setValue(
							//		customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobglbdef2()));
							bean.getBeforenewPostLevel().setValue(customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobglbdef2()));
							// 岗位等级
							//bean.getPostGrade().setValue(
							//		customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobglbdef3()));
							bean.getBeforenewpostGrade().setValue(customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobglbdef3()));

							// 一级基准岗位
							bean.getBasePostA()
									.setValue(uecPostDao.getUecPostNameByPkBostPost(qryUecPsnjob.getJobglbdef22()));
							// 二级基准岗位
							bean.getBasePostB()
									.setValue(uecPostDao.getUecPostNameByPkBostPost(qryUecPsnjob.getJobglbdef23()));
							// 三级基准岗位
							bean.getBasePostC()
									.setValue(uecPostDao.getUecPostNameByPkBostPost(qryUecPsnjob.getJobglbdef24()));
							// 任职方式
							bean.getAppointmentWay()
									.setValue(customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobmode()));
							// 免职方式
							bean.getDismissalWay().setValue(
									customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getDeposemode()));
							// 职务名称
							bean.getPositionName().setValue(qryUecPsnjob.getJobglbdef4());
							// 是否在岗--POSTSTAT
							if ("Y".equals(qryUecPsnjob.getPoststat())) {
								bean.getIsOnDuty().setValue("是");
							} else if ("N".equals(qryUecPsnjob.getPoststat())) {
								bean.getIsOnDuty().setValue("否");
							} else {
								bean.getIsOnDuty().setValue(null);
							}
							// 任职原因
							bean.getAppointmentReason().setValue(qryUecPsnjob.getJobglbdef5());
							// 任职文号
							bean.getAppointmentNumber().setValue(qryUecPsnjob.getJobglbdef6());
							// 免职文号
							bean.getDismissalNumber().setValue(qryUecPsnjob.getJobglbdef7());
							// 任本岗位层级时间
							bean.getRenaultLevelTime().setValue(qryUecPsnjob.getJobglbdef8());
							// 现职时间
							bean.getRenaultGradeTime().setValue(qryUecPsnjob.getJobglbdef14());
							// 进入本单位来源
							bean.getEnterUnitDate().setValue(
									customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobglbdef10()));
							// 是否小CEO
							if ("Y".equals(qryUecPsnjob.getJobglbdef21())) {
								bean.getIsCEO().setValue("是");
							} else if ("N".equals(qryUecPsnjob.getJobglbdef21())) {
								bean.getIsCEO().setValue("否");
							} else {
								bean.getIsCEO().setValue(null);
							}
							// 人员类别备注
							bean.getPersonnelCategoryMemo().setValue(qryUecPsnjob.getJobglbdef13());
							// 领导职务名称
							bean.getLeadingPosition().setValue(
									customFilesManager.findCustomFilesByPkdefdoc(qryUecPsnjob.getJobglbdef25()));
							// 负责部门
							bean.getResponsibleDepartment().setValue(
									uecOrgDeptManager.getUecOrgDeptNameByPkDept(qryUecPsnjob.getJobglbdef26()));
							// 备注
							// bean.getOldRemark().setValue();
						}
						uecOutStaffInfo = qryUecOutStaffInfo;
					}
				}
			}
		});
	}

	/**
	 * 人员调配申请流程发起按钮
	 * 
	 * @throws Exception
	 * 
	 * @throws Exception
	 */
	public void onViewUecOutStaffAdjustApply() throws Exception {
		if (null == uecOutStaffInfo || null == uecOutStaffInfo.getOutStaffId()) {
			ZkUtil.showError("未获取人员！", "错误提示");
			return;
		}
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 判断是否入职
		UecTrial qryUecTrial = UecTrial.newInstance();
		qryUecTrial.setStaffId(qryUecOutStaffInfo.getOutStaffId());
		qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
		// 判断 人员试用情况是否为null
		if (StrUtil.isNullOrEmpty(qryUecTrial)) {
			ZkUtil.showInformation("此人未入职：试用信息为空", "温馨提示");
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		/**
		 * 生效时间： 生效时间为调配后新的的任职信息的开始时间，为调配前旧的任职信息的结束时间
		 */
		String effetDate = DateUtil.dateToStr(bean.getEffectDate().getValue());
		/**
		 * 1：盟市内转正：组织不变，部门可变、岗位可变 2：盟市间转正：组织可变、部门可变、岗位可变
		 */
		// 调配时修改部门信息
		if (null != qryUecPsnjob) {
			if (null != findUecOrgDept) {
				/**
				 * 1:区本部的人员可以对某个盟市的人员进行跨组织转正，即转正后所在的组织跟部门非当前组织部门
				 * 2:某个盟市的主管对该盟市人员进行转正，只能是组织不动、调部门和岗位
				 */
				// 更新人员基本信息中的组织
				
				if(StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkHrorg())) {
					uecOutStaffInfo.setPkHrorg(findUecOrgDept.getPkOrg());
				}
				if (uecOutStaffInfo.getPkHrorg().equals(findUecOrgDept.getPkOrg())) {
					outStaffInfoUpdateFlag = true;
					uecOutStaffInfo.setPkHrorg(findUecOrgDept.getPkOrg());
					uecOutStaffInfo.setModifier(currentName);
					uecOutStaffInfo.setModifiedTime(DateUtil.getCurrenDate());
				} else {
					outStaffInfoUpdateFlag = false;
				}
				// 如果更改任职信息表中的组织、部门、岗位，即新增一条任职信息
				uecPsnjob = new UecPsnjob();
				// 同步员工的id
				uecPsnjob.setStaffId(uecOutStaffInfo.getOutStaffId());
				// 外包标识
				uecPsnjob.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
				// 同步集团默认主键
				uecPsnjob.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
				// 获取组织
				uecPsnjob.setPkOrg(findUecOrgDept.getPkOrg());
				uecPsnjob.setPkHrorg(findUecOrgDept.getPkOrg());
				// 获取部门
				uecPsnjob.setPkDept(findUecOrgDept.getPkDept());
				// 获取岗位
				if (null != uecPost) {
					uecPsnjob.setPkPost(uecPost.getPkPost());
				}
				if (null != uecBdPsnclTree) {
					String pkpsncl = uecBdPsnclTree.getPkPsncl();
					uecPsnjob.setPkPsncl(pkpsncl);
				}
				// 调配原因
				uecPsnjob.setTrnsreason((String) bean.getTrnsreason().getSelectedItem().getValue());
				// 获取岗位序列
				if(null != postSequence) {
					uecPsnjob.setPkPostseries(postSequence);
				}
				// 获取岗位层级
				//uecPsnjob.setJobglbdef2((String) bean.getJobglbdef2().getSelectedItem().getValue());
				uecPsnjob.setJobglbdef2(bean.getNewPostLevel().getUecBdDefdocs().get(0).getPkDefdoc());
				// 获取岗位等级
				//uecPsnjob.setJobglbdef3((String) bean.getJobglbdef3().getSelectedItem().getValue());
				uecPsnjob.setJobglbdef3(bean.getNewpostGrade().getUecBdDefdocs().get(0).getPkDefdoc());
				//人员类别
				if(bean.getOutPsncl().getUecBdPsncls()!=null) {
					uecPsnjob.setPkPsncl(bean.getOutPsncl().getUecBdPsncls().get(0).getPkPsncl());
					}
				/**
				 * 一二三级基准岗位
				 */
				if(pkBasePostC != null) {
					//三级基准岗位
					uecPsnjob.setJobglbdef24(pkBasePostC);
				}
				if(null != pkBasePostB) {
					// 二级基准岗
					uecPsnjob.setJobglbdef23(pkBasePostB);
				}
				if(null != pkBasePostA) {
					// 一级基准岗
					uecPsnjob.setJobglbdef22(pkBasePostA);
				}
				// 获取任职方式
				uecPsnjob.setJobmode((String) bean.getJobmode().getSelectedItem().getValue());
				// 获取免职方式
				uecPsnjob.setDeposemode((String) bean.getDeposemode().getSelectedItem().getValue());
				// 获取职务名称
				uecPsnjob.setJobglbdef4(bean.getJobglbdef4().getValue());
				// 是否在岗
				int poststat = bean.getPoststat().getSelectedIndex();
				if (0L == poststat) {
					uecPsnjob.setPoststat("Y");
				} else {
					uecPsnjob.setPoststat("N");
				}
				// 获取任职原因
				uecPsnjob.setJobglbdef5(bean.getJobglbdef5().getValue());
				// 获取任职文号
				uecPsnjob.setJobglbdef6(bean.getJobglbdef6().getValue());
				// 获取免职文号
				uecPsnjob.setJobglbdef7(bean.getJobglbdef7().getValue());
				// 获取任本岗位层级时间
				uecPsnjob.setJobglbdef8(DateUtil.dateToStr(bean.getJobglbdef8().getValue()));
				// 现职时间
				uecPsnjob.setJobglbdef14(DateUtil.dateToStr(bean.getJobglbdef14().getValue()));
				// 获取进入本单位来源
				uecPsnjob.setJobglbdef10((String) bean.getJobglbdef10().getSelectedItem().getValue());
				// 是否小CEO
				int jobglbdef21 = bean.getJobglbdef21().getSelectedIndex();
				if (0L == jobglbdef21) {
					uecPsnjob.setJobglbdef21("Y");
				} else if(1L == jobglbdef21){
					uecPsnjob.setJobglbdef21("N");
				}
				// 获取人员类别备注
				uecPsnjob.setJobglbdef13(bean.getJobglbdef13().getValue());
				// 获取领导职务
				String jobGlbdef25 = (String) bean.getJobglbdef25().getSelectedItem().getValue();
				uecPsnjob.setJobglbdef25(jobGlbdef25);
				// 负责部门
				/**
				 * 如果领导职务为：部门第一负责人或者部门其他负责人，负责部门取部门
				 */
				if (HEAD_OF_DEPARTMENT.equals(jobGlbdef25) || OTHER_OF_DEPARTMENT.equals(jobGlbdef25)) {
					uecPsnjob.setJobglbdef26(findUecOrgDept.getPkDept());
				}

				// 调配后任职开始时间
				uecPsnjob.setBegindate(effetDate);
				// 人员状态设置为:新进
				uecPsnjob.setPsntype(0L);
				// 是否主职：是
				uecPsnjob.setIsmainjob("Y");
				// 是否结束:否
				uecPsnjob.setEndflag("N");
				
				// 是否最新记录:是
				uecPsnjob.setLastflag("Y");
				// 修改 相应的创建人、创建时间
				uecPsnjob.setCreator(currentName);
				uecPsnjob.setCreationtime(DateUtil.getCurrenDate());
			}
			/**
			 * 生效时间不为空，将调配前的任职信息的结束日期更新为生效时间
			 */
			qryUecPsnjob.setEnddate(effetDate);
			// 人员状态设置为:退出
			qryUecPsnjob.setPsntype(2L);
			// 是否主职：否
			qryUecPsnjob.setIsmainjob("N");
			// 是否结束：是
			qryUecPsnjob.setEndflag("Y");
			//是否最新记录：否
			qryUecPsnjob.setLastflag("N");
			qryUecPsnjob.setModifier(currentName);
			qryUecPsnjob.setModifiedtime(DateUtil.getCurrenDate());
		}

		// 发起流程
		if (ApprovalUtil.getActivityFlag()) {

			// 判断该人员是否在审状态，如果是在审状态不允许再次发起此流程
			boolean isOnApproval = ApprovalUtil.isOnApproval(ApprovalConstants.OUT_STAFF_ADJUST_PROC_DEF_ID,
					uecOutStaffInfo.getOutStaffId());
			if (isOnApproval) {
				ZkUtil.showError("在审状态不允许发起！", "错误提示");
				return;
			}
			if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
				ZkUtil.showError("当前登录用户不存在!", "错误提示");
				return;
			}

			// 数据入库 : 修改 uec_out_taff_info
			// 一条记录，修改uec_psnjob一条记录,添加uec_psnjob一条记录
			Long outStaffIdHis = uecOutStaffInfo.getOutStaffId();
			Long uecPsnjobIdHis = qryUecPsnjob.getPsnjobId();

			uecOutStaffInfo.addStatusCdIS3();
			Long outStaffId = uecOutStaffInfo.getOutStaffId();

			qryUecPsnjob.addStatusCdIS3();
			Long uecPsnjobIdModify = qryUecPsnjob.getPsnjobId();

			uecPsnjob.addStatusCdIS3();
			Long uecPsnjobIdAdd = uecPsnjob.getPsnjobId();

			String formTheme = "盟市外包人员调配流程";

			String startFlowJson = "{" + " formTheme: '" + formTheme + "',"
					+ " formContent: '调配人员:" + uecOutStaffInfo.getPsnName()
					+ "'," + " procDefId: '"
					+ ApprovalConstants.OUT_STAFF_ADJUST_PROC_DEF_ID + "',"
					+ " urgent: '1'," + " atiActCategoryId: '"
					+ ApprovalConstants.ADJUST_CATEGORY_ID + "',"
					+ " procDefKey: '" + ApprovalUtil.OUT_STAFF_ADJUST_EDIT
					+ "'," + " formSender: '" + currentName + "',"
					+ " primaryId: '{" + "uec_out_staff_info_his:"
					+ outStaffIdHis + ",uec_out_staff_info:" + outStaffId
					+ ",uec_psnjob_his:" + uecPsnjobIdHis + ",uec_psnjob_modi:"
					+ uecPsnjobIdModify + ",uec_psnjob_add:" + uecPsnjobIdAdd
					+ "'}";

			String procInstId = null;
			try {
				procInstId = ApprovalUtil.startApproval(startFlowJson);
			} catch (Exception e) {
				// 流程发起失败 插入的数据 变为失效状态
				uecOutStaffInfo.removeOnly();
				qryUecPsnjob.removeOnly();
				onReset();
				e.printStackTrace();
			}
			if (StrUtil.isNullOrEmpty(procInstId)) {
				uecOutStaffInfo.removeOnly();
				qryUecPsnjob.removeOnly();
				onReset();
				ZkUtil.showError("流程发起失败", "错误提示");
				return;
			}

			// 第一审批人如果是发起人直接到人力审批环节
			ApprovalUtil.isCurrentAssignNames(procInstId);

			ApprovalUtil.showNextAssignee(procInstId, formTheme);

			onReset();

		} else {
			if (outStaffInfoUpdateFlag) {
				uecOutStaffInfo.updateOnly();
			}
			// 调配前的任职信息更新
			qryUecPsnjob.updateOnly();
			// 调配后的任职信息新增入库
			
			uecPsnjob.addOnly();
			ZkUtil.showInformation("数据保存成功", "温馨提示");
			onReset();
		}
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getTrnsreason().getSelectedItem().getValue())) {
			return "请选择调配原因";
		} else if (StrUtil.isNullOrEmpty(bean.getEffectDate().getValue())) {
			return "请选择生效日期";
		} else if (StrUtil.isNullOrEmpty(bean.getOutStaffPkDept().getValue())) {
			return "请选择调配后部门";
		} else if (StrUtil.isNullOrEmpty(bean.getUecPostBandboxExt().getValue())) {
			return "请选择调配后岗位";
		} else if (StrUtil.isNullOrEmpty(bean.getOutPsncl().getValue())) {
			return "请选择调配后人员类别";
		} 
		/*else if (StrUtil.isNullOrEmpty(bean.getJobglbdef2().getSelectedItem().getValue())) {
			return "请选择调配后岗位层级";
		} else if (StrUtil.isNullOrEmpty(bean.getJobglbdef3().getSelectedItem().getValue())) {
			return "请选择调配后岗位等级";
		} */
		
		else if (StrUtil.isNullOrEmpty(bean.getNewpostGrade().getUecBdDefdocs())) {
			return "请选择调配后岗位等级";
		} else if (StrUtil.isNullOrEmpty(bean.getNewPostLevel().getUecBdDefdocs())) {
			return "请选择调配后岗位层级";
		}else if (StrUtil.isNullOrEmpty(bean.getJobglbdef10().getSelectedItem().getValue())) {
			return "请选择进入本单位来源";
		}
		return null;
	}

	/**
	 * 重置按钮
	 */
	public void onReset() {
		clearVariable();
		// 清除页面元素
		PubUtil.clearComponent(this);
		// 重置自定义组件
		this.bean.getOutStaffPkDept().setValue(null);
		this.bean.getUecPostBandboxExt().getBean().getUecPostListboxExt().onReset();
		Events.postEvent("onCleanUecDeptSelect", this.bean.getUecPostBandboxExt().getBean().getUecPostListboxExt(),
				null);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryUecOutStaffInfo = null;
		qryUecPsnjob = null;
		uecOutStaffInfo = null;
		uecOutStaffInfoVo = null;
	}

	/**
	 * 绑定Listbox
	 */
	public void bandListbox() {
		// 调配原因
		ListboxUtils.rendererForEdit(bean.getTrnsreason(),
				NodeVoUtil.getDictionaries(UecConstants.CHANGR_REASON, UecConstants.CHANGR_REASON_PID_ZH));
		// 人员类别--PSNCL_PK_ORG
		ListboxUtils.rendererForEdit(bean.getPkPsncl(), NodeVoUtil.getDictionariesPsncl(UecConstants.PSNCL_PK_ORG));
		// 岗位层级--JOBGLBDEF2
		ListboxUtils.rendererForEdit(bean.getJobglbdef2(), NodeVoUtil.getDictionaries(UecConstants.POST_HIERARCHY));
		// 岗位等级--Jobglbdef3
		ListboxUtils.rendererForEdit(bean.getJobglbdef3(), NodeVoUtil.getDictionaries(UecConstants.POST_LEVEL));
		// 进入本单位来源jobglbdef10-
		ListboxUtils.rendererForEdit(bean.getJobglbdef10(),
				NodeVoUtil.getDictionaries(UecConstants.ASSCESS_SOURCE_UTIL));
		// 任职方式
		ListboxUtils.rendererForEdit(bean.getJobmode(), NodeVoUtil.getDictionaries(UecConstants.WAY_OF_TENURE));
		// 免职方式
		ListboxUtils.rendererForEdit(bean.getDeposemode(), NodeVoUtil.getDictionaries(UecConstants.WAY_OF_DISMISSAL));
		// 领导职务
		ListboxUtils.rendererForEdit(bean.getJobglbdef25(), NodeVoUtil.getDictionaries(UecConstants.LEADING_POSITION));
	}

}
